package com.sicheng.蓝桥.练习题;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/4/8 20:17
 */
public class 倍数问题 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }

        Arrays.sort(nums);
        long res = 0;
        long temp;
        int start = 0;
        if (n > 10000)
            start =n- n / 10;
        for (int i = start; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length - 1; j++) {
                for (int l = j + 1; l < nums.length; l++) {
                    temp = nums[i] + nums[j] + nums[l];
                    if (temp % k == 0)
                        res = Math.max(temp, res);
                }
            }
        }

        System.out.println(res);
    }
}
